from flask import *
import pandas as pd
from itertools import combinations
import requests
app = Flask(__name__)

# 加载模型
frequent_itemsets = pd.read.pickle('./frequent_itemsets.pkl')
rules = pd.read.pickle('./rules.pkl')

@app.route("/recomment", methods=['POST'])
def recommend():
    # 接受传过来的参数
    data = request.json.get('items', [])

    # 生成关联规则推荐
    recommendDations = []
    for idx, rule in rules.iterrows():
        antecedents = list(rule['antecedents'])
        consequents = list(rule['consequents'])

        # 检查规则前件是否跟输入匹配
        if set(antecedents).issubset(set(data)):
            recommendDations.extend(consequents)

    recommendDations = list(set(recommendDations) - set(data))
    return jsonify({'recommendDations':recommendDations})

if __name__ == '__main__':
    app.run()